<template>
  <div></div>
</template>

<script>
import * as Cesium from "cesium";
// import * as WebGLConstants from 'cesium/Source/Core/WebGLConstants';
export default {
  setup() {},
  methods: {
    loadTileset() {
      // let IsYaPing = [true, true, false, false]; //[是否开启编辑，是否开启压平，是否开启裁剪，是否开启淹没]

      const viewer = window.viewer;

      // var vertexShader = "uniform mat4 czm_projection\n;";
      // vertexShader += "attribute vec3 position;\n";
      // vertexShader += "uniform mat4 u_modelViewMatrix;\n";
      // // vertexShader += "uniform bool u_bFlatten;\n";
      // vertexShader += "uniform sampler2D u_polygonTexture;\n";
      // vertexShader += "uniform vec4 u_polygonBounds;\n";
      // vertexShader += "attribute vec2 textureCoordinates;\n";
      // vertexShader += "varying vec2 v_textureCoordinates;\n";

      // // 判断顶点与外包矩形的关系
      // vertexShader +=
      //   "bool isPointInBound(vec2 point, vec4 bounds)\n" +
      //   "{\n" +
      //   "    return (point.x>bounds.x&&point.x<bounds.z&&point.y<bounds.w&&point.y>bounds.y);\n" +
      //   "}\n" +
      //   "float unpackDepth(const in vec4 rgba_depth)\n" +
      //   "{\n" +
      //   "    const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / (255.0 * 255.0), 1.0 / (255.0 * 255.0 * 255.0));\n" +
      //   "    float depth=dot(rgba_depth, bitShifts);\n" +
      //   "    return depth;\n" +
      //   "}\n";

      // vertexShader += "    void vertexMain() {\n";
      // // vertexShader += "    if(u_bFlatten) {\n";
      // vertexShader += "        vec4 viewPos = vec4(position.xyz, 1.0);\n";
      // vertexShader += "        if (isPointInBound(viewPos, u_polygonBounds)) {\n";
      // vertexShader += "        vec2 texCoord;\n";
      // vertexShader += "        texCoord.x = (viewPos.x-u_polygonBounds.x)/(u_polygonBounds.z-u_polygonBounds.x);\n";
      // vertexShader += "        texCoord.y = (viewPos.y-u_polygonBounds.w)/(u_polygonBounds.y-u_polygonBounds.w);\n";
      // vertexShader += "        float texelSize = 1.0/4096.0;\n";
      // vertexShader += "        float depth0 = unpackDepth(texture2D(u_polygonTexture, texCoord.xy));\n";
      // vertexShader += "        float depth1 = unpackDepth(texture2D(u_polygonTexture, texCoord.xy + vec2(-texelSize, 0.0)));\n";
      // vertexShader += "        float depth2 = unpackDepth(texture2D(u_polygonTexture, texCoord.xy + vec2(texelSize, 0.0)));\n";
      // vertexShader += "        float depth3 = unpackDepth(texture2D(u_polygonTexture, texCoord.xy + vec2(0.0, -texelSize)));\n";
      // vertexShader += "        float depth4 = unpackDepth(texture2D(u_polygonTexture, texCoord.xy + vec2(0.0, texelSize)));\n";
      // vertexShader += "        float minDepth = min( min( min( min( depth0, depth1), depth2), depth3), depth4);\n";
      // vertexShader += "        if(minDepth*5000.0 > 1.0) {\n";
      // vertexShader += "            float depth = max( max( max( max( depth0, depth1), depth2), depth3), depth4);\n";
      // vertexShader += "            viewPos.z = depth*5000.0;\n";
      // vertexShader += "        }\n";
      // vertexShader += "        gl_Position = czm_projection* u_modelViewMatrix* vec4(viewPos.xyz, 1.0);\n";
      // vertexShader += "        }\n";
      // vertexShader += "        else {\n";
      // vertexShader += "            gl_Position = czm_projection* u_modelViewMatrix* vec4(position.xyz, 1.0);\n";
      // vertexShader += "        }\n";
      // vertexShader += "    }\n";
      // // vertexShader += "    else {\n";
      // // vertexShader += "        gl_Position = czm_projection* u_modelViewMatrix* vec4(position.xyz, 1.0);\n";
      // // vertexShader += "    }\n";
      // vertexShader += "    v_textureCoordinates = textureCoordinates;\n";
      // vertexShader += "}\n";

      var vertexShader=`
      bool isPointInBound(vec4 point, vec4 bounds) {
        return (point.x>bounds.x&&point.x<bounds.z&&point.y<bounds.y&&point.y>bounds.w);
      }
      float unpackDepth(const in vec4 rgba_depth) {
        const vec4 bitShifts = vec4(1.0, 1.0 / 255.0, 1.0 / (255.0 * 255.0), 1.0 / (255.0 * 255.0 * 255.0));
        float depth = dot(rgba_depth, bitShifts);
        return depth;
      }

      void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput)
      {
        vec4 viewPos = vec4(vsInput.attributes.positionMC, 1.0);
        if(isPointInBound(viewPos, u_polygonBounds)){
          vsOutput.positionMC.z = 1.0;
        }
      }
    `;
      
      
      //   let customShader = new Cesium.CustomShader({
      //     vertexShaderText: `
      //   void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput)
      //   {
      //     vsOutput.positionMC.z = 0.1 * vsInput.attributes.positionMC.z;
      //   }
      // `,
      //   });

      let customShader = new Cesium.CustomShader({
        uniforms: {
          u_polygonBounds: {
            type: Cesium.UniformType.VEC4,
            value: new Cesium.Cartesian4.fromArray([113.949051,22.524725,1000, 30000.0]),
          }
        },
        vertexShaderText: vertexShader,
      });

      let url = "http://192.9.102.125:8100/FUTIAN_MASH/tileset.json";
      let key = "";
      // let key =
      //   "ofxq28JQqqNWJG5/BVVqc76yP4WNnOnfqeENF/REpNbHfRbQIvhtbIX+jQmWilTb";

      const resource = new Cesium.Resource({
        url: url,
      });

      if (key) {
        resource.headers = {
          "szvsud-license-key": key,
        };
      }

      const tileset = new Cesium.Cesium3DTileset({
        url: resource,
        customShader: customShader,
        enableModelExperimental: true,
      });
      viewer.scene.primitives.add(tileset);
      viewer.zoomTo(tileset);
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.loadTileset();
    });
  },
};
</script>